const { notifyModel } = require('../../model');
const { hashCode } = require('../../services/utils');

module.exports = {
  list: function (req,res) {
    const { org } = req.decode;
    const { title, sort, page, limit } = req.query;
    const attributes = ['id', 'title', 'content', 'author', 'link', 'startTime', 'endTime', 'updatedAt', 'createdAt'];
    let where = { org: org };
    let LIMIT = null, OFFSET = null;

    if (limit && page) {
      LIMIT = parseInt(limit);
      OFFSET = (parseInt(page) - 1) * LIMIT;
    } else {
      LIMIT = null;
      OFFSET = null;
    }

    notifyModel.findByFilterLimit(attributes, where, LIMIT, OFFSET).then(function (result) {
      if (result.length === 0) {
        return res.send({
          code: 20000,
          data: {
            total: 0,
            items: []
          }
        })
      }
      let mockList = result.filter(item => {
        return !(title && item.title.indexOf(title) < 0);
      });
      if (sort === '-id') {
        mockList = mockList.reverse()
      }
      for (const item of mockList) {
        item['unique'] = hashCode(item.updatedAt + item.content + item.title)
      }
      // const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));

      return res.send({
        code: 20000,
        data: {
          total: mockList.length,
          items: mockList
        }
      });

    }).catch(function (err) {
      console.log(err);
      return res.send({
        code: 10000,
        message: '通知列表查询失败'
      });
    });
  },
  create: function (req,res) {
    const { org } = req.decode;
    const { title, content, time, author } = req.body;

    const config = {
      org: org,
      title: title,
      content: content,
      startTime: time[0],
      endTime: time[1],
      author: author,
      createdAt: Date.now(),
      updatedAt: Date.now()
    };
    console.log(config);

    notifyModel.create(config).then(function () {
        res.send({
          code: 20000
        })
    }).catch(function (err) {
      console.log(err);
      res.send({
        code: 10000
      })
    })
  },
  delete: function (req,res) {
    const org = req.decode.org;
    const { id } = req.body;

    const config = {
      id: id,
      org: org
    };

    notifyModel.delete(config).then(() => {
      return res.send({
        code: 20000,
        message: '通知删除成功'
      });
    }).catch(err => {
      console.log(err);
      return res.send({
        code: 10000,
        message: '通知删除失败'
      });
    });
  },
  update: function (req,res) {
    const org = req.decode.org;
    const { id, title, content, author } = req.body;
    const attributes = { title: title, content: content, author: author, updatedAt: Date.now() };
    const where = { id: id, org: org };
    notifyModel.update(attributes,where).then(function () {
      res.send({
        code: 20000
      })
    }).catch(function (err) {
      console.log(err);
      res.send({
        code: 10000
      })
    })
  }
};
